package com.nl.controller.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.mapper.CommonMapper;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.web.bean.SmsCitySignInfo;
import com.nl.controller.web.mapper.SmsCitySignMapper;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class SmsCitySignService {
	@Autowired
    private CommonService commonService;
	@Autowired
	private SmsCitySignMapper smsCitySignMapper;
	@Autowired
	private CommonMapper commonMapper;
	@SystemServiceLog(value = false, description = "查询地市标识")
    public PageInfo<SmsCitySignInfo> find(int page, int limit, String cityId, String signId) {
        PageHelper.startPage(page, limit);
        List<SmsCitySignInfo> list = new ArrayList<>();
        try {
            list = smsCitySignMapper.getDataList(cityId,signId);
            commonService.createSmsOperLog("4", "地市标识查询", "0", "地市标识管理");
        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("4", "地市标识查询", "1", "地市标识管理");
            log.error("地市标识查询");
            throw new SmsException("地市标识查询异常：" + cityId+","+signId);
        }
        PageInfo<SmsCitySignInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
	@Transactional
	@SystemServiceLog(value = false, description = "修改地市标识")
    public int modifyCitySign(String cityId, String signId) {
        int result = 0;
        SmsCitySignInfo smsCitySignInfo = new SmsCitySignInfo();//获取修改前数据
        String seqId = commonMapper.getLogSeqId();
        try {
        	smsCitySignInfo = smsCitySignMapper.getDataByCityId(cityId);
        	result = smsCitySignMapper.modifyCitySign(cityId,signId);
        	//插入地市标识历史表
        	smsCitySignInfo.setSeqId(seqId);
        	smsCitySignMapper.insertCitySignHis(smsCitySignInfo);
        	commonService.createSmsUpdateOperLog("3", "地市标识修改"+cityId, "0", "地市标识管理", seqId);
        } catch (Exception e) {
        	TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            commonService.createSmsOperLog("3", "地市标识修改"+cityId, "1", "地市标识管理");
            log.error("地市标识修改");
            throw new SmsException("地市标识修改异常：" + cityId+","+signId);
        }
        return result;
    }
	@SystemServiceLog(value = false, description = "历史地市标识查询")
	public SmsCitySignInfo findCitySignHis(String seqId){
		SmsCitySignInfo smsCitySignInfo = new SmsCitySignInfo();
		try {
			smsCitySignInfo = smsCitySignMapper.findCitySignHis(seqId);
			commonService.createSmsOperLog("4", "历史地市标识查询", "0", "日志管理");
		} catch (Exception e) {
			e.printStackTrace();
			commonService.createSmsOperLog("4", "历史地市标识查询", "1", "日志管理");
            log.error("历史地市标识查询");
            throw new SmsException("历史地市标识查询异常："+seqId);
		}
		return smsCitySignInfo;
	}
}
